jest test 測試要等待的東西


https://github.com/mrdulin/react-act-examples/blob/master/sync.md

  describe('page buttons', () => {
    it('should change page number when click the next button', async () => {
      // Given
      let wrapper;

      act(() => {
        wrapper = mount(<PDFViewer pdf={pdfUrl} />);
        wrapper.find('Document').props().onLoadSuccess({ numPages: 2 });
      });

      const buttonRight = wrapper.find('.PDFViewer__button-right');
      expect(wrapper.find('Page').props().pageNumber).toEqual(1);

      // When
      act(() => {
        buttonRight.simulate('click');
      });
      wrapper.update();

      // Then
      expect(wrapper.find('Page').props().pageNumber).toEqual(2);
    });

    it('should change page number when click the prev button', async () => {
      // Given
      let wrapper;

      act(() => {
        wrapper = mount(<PDFViewer pdf={pdfUrl} />);
        wrapper.find('Document').props().onLoadSuccess({ numPages: 2 });
      });

      const buttonRight = wrapper.find('.PDFViewer__button-right');
      const buttonLeft = wrapper.find('.PDFViewer__button-left');
      expect(wrapper.find('Page').props().pageNumber).toEqual(1);
      act(() => {
        buttonRight.simulate('click');
      });
      wrapper.update();

      // When
      act(() => {
        buttonLeft.simulate('click');
      });
      wrapper.update();

      // Then
      expect(wrapper.find('Page').props().pageNumber).toEqual(1);
    });
  });

Ref

Alex Krolick: Testing Async Components
https://www.youtube.com/watch?v=b8SOFNc_X_A&list=PLRvKvw42Rc7NEul9GviGijdznGbh8yl4Z&index=5








你可能感興趣的文章

跟你朋友介紹 Git

跟你朋友介紹 Git

[Vue 學習筆記(一)]學習資源

[Vue 學習筆記(一)]學習資源

AppWorks School Batch #16 Front-End Class 學習筆記&心得(駐點階段四:個人專案~Sprint 4)

AppWorks School Batch #16 Front-End Class 學習筆記&心得(駐點階段四:個人專案~Sprint 4)






留言討論